﻿using System;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Reflection;

namespace EQEmu.Database
{
    public static class EntityObjectExtensions
    {
        public static EntityObject CopyFrom(this EntityObject obj, EntityObject otherObject)
        {
            PropertyInfo[] srcFields = otherObject.GetType().GetProperties(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);

            PropertyInfo[] destFields = obj.GetType().GetProperties(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty);

            foreach (var property in srcFields.Where(x => x.Name != "EntityKey" && x.Name != "EntityState"))
            {                
                var dest = destFields.FirstOrDefault(x => x.Name == property.Name);
                if (dest != null)
                    dest.SetValue(obj, property.GetValue(otherObject, null), null);
            }

            return obj;
        }
    }
}
